package com.example.allinone.homework08.fragment2;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.allinone.R;
import com.example.allinone.homework08.User;

import java.util.List;

public class Fragment2 extends Fragment implements View.OnClickListener{
    private Button imp ;
    private Button sel ;
    private TextView username ;
    private TextView age ;
    private TextView result ;
    private TextView low ;
    private TextView high ;

    public Fragment2(){
        super(R.layout.fragment2);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        imp = (Button) getActivity().findViewById(R.id.imp_f2);
        sel = (Button) getActivity().findViewById(R.id.sel_f2);
        username = (TextView) getActivity().findViewById(R.id.username_f2) ;
        age = (TextView) getActivity().findViewById(R.id.age_f2) ;
        result = (TextView) getActivity().findViewById(R.id.result_f2) ;
        low = (TextView)getActivity().findViewById(R.id.low_f2) ;
        high = (TextView)getActivity().findViewById(R.id.high_f2) ;
        imp.setOnClickListener(this);
        sel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imp_f2:
                addUser();
                break;
            case R.id.sel_f2:
                select();
                break;
        }
    }

    private void addUser(){
        User user = getUser() ;
        if(user!=null){
            MyRoom db = MyRoom.getInstance(getActivity());
            db.userDao().insert(user);
            Toast.makeText(getContext(),user.toString(),Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getContext(),"年龄输入有误",Toast.LENGTH_SHORT).show();
        }
    }


    private User getUser(){
        String use = username.getText().toString() ;
        try{
            int ag = Integer.parseInt(age.getText().toString()) ;
            return new User(use,ag);
        }catch (NumberFormatException e){
            return null ;
        }
    }

    private void select(){
        Integer hi = "".equals(high.getText().toString()) ? 100 : Integer.parseInt(high.getText().toString());
        Integer lo = "".equals(low.getText().toString()) ? 0 : Integer.parseInt(low.getText().toString());
        MyRoom db = MyRoom.getInstance(getActivity());
        List userlist = db.userDao().seachUserByAge(lo,hi);
        StringBuilder stringBuilder = new StringBuilder() ;
        int counter = 0 ;
        for (Object o : userlist) {
            counter++ ;
            User user = (User)o ;
            stringBuilder.append(user.toString()).append("\n") ;
        }
        result.setText("一共有"+counter+"条记录：\n"+stringBuilder.toString());
    }
}
